#include "login.h"
#include "ui_login.h"
#include<QDebug>
#include"mainwindow.h"

void parseData(QString response, QVector<Body>&arrbody, QByteArray& finalData)
{
     //解析请求状态码
    int end1=response.indexOf("\\r\\n",0);
    int mid1=response.indexOf(':',0);
    if(end1!=-1)
    {
        QString key1=response.left(mid1);
        QString value1=response.mid(mid1+2,end1-mid1-3);
        arrbody.push_back(Body(key1,value1));
    }
    int end2=response.indexOf("\\r\\n",end1+4);
    int mid2=response.indexOf(':',end1+4);
    
    int end3=response.indexOf("\\r\\n",end2+4);
    int mid3=response.indexOf(':',end2+4);
    if(end3!=-1)
    {
        QString key3=response.mid(end2+4,mid3-end2-4);
        QString value3=response.mid(mid3+2,end3-mid3-3);
        arrbody.push_back(Body(key3,value3));
    }
    //找到\0
    int end4=response.indexOf("\\0",end3+4);
    finalData.append(response.mid(end3+4,end4-end3-4));
}

login::login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);//去掉标题栏
    setFixedSize(1300,880);
    ui->loginicon->setFixedSize(400,400);
    ui->loginicon->move(450,0);
    client =new tcpclient();
    ui->tishi->hide();
    this->timer=new QTimer(this);

    connect(client->m_socket,&QTcpSocket::readyRead,this,[=](){     //当有数据读时
           QByteArray array;
           int ret=1;
           while(ret>0)
           {
              array.append(client->m_socket->readAll());
              ret=client->m_socket->readAll().size();
           }
           client->Receiving.append(array);
    });

    connect(client->getsocket(),&QTcpSocket::disconnected,this,[=](){   //关闭套接字
          client->getsocket()->close();  //关闭套接字
          //解析数据
          //Responsecode:{ok}\r\nusernameid:{1373474316}\r\nregister:{OK}\r\n黎英杰\0
          QString response=client->getnetworkdata();
          QVector<Body>arrbody;
          QByteArray finalData;
          parseData(response,arrbody,finalData);
          client->clearnetwork();

          //判断用户密码账户是否输入正确
          if(arrbody[2].value=="OK")    //成功
          {
                qDebug()<<passwd<<" "<<nameid<<endl;
                MainWindow *m=new MainWindow(nameid,finalData.data(),passwd);
                m->show();
                this->close();
          }
          else                          //失败
          {
              //提示用户输入错误
              timer->start(1000);
              ui->tishi->show();
          }
    });

    connect(timer,&QTimer::timeout,this,[&](){
          timer->stop();
          ui->tishi->hide();
    });
}

login::~login()
{
    delete ui;
    delete client;
}
/*Responsecode:{ok}\r\n
  usernameid:{1373474316}\r\n
  register:{OK}\r\n
  黎英杰\0
*/



void login::mousePressEvent(QMouseEvent *event){
     last = event->globalPos();
}

void login::mouseMoveEvent(QMouseEvent *event){
    int dx = event->globalX() - last.x();
    int dy =  event->globalY() - last.y();
    last = event->globalPos();
    move(x()+dx, y()+dy);
}

void login::on_pushButton_clicked()
{
   this->close();
}
//用户登录
//usrnameid:{1373474316}\r\npassword:{lyj137347}\r\nregister:{}\r\n
void login::on_register_2_clicked()
{
    if(ui->nameid->text()==nullptr || ui->password->text()==nullptr){
        return ;
    }

    client->write("usrnameid:{");
    client->write(ui->nameid->text());
    client->write("}\\r\\npassword:{");
    client->write(ui->password->text());
    client->write("}\\r\\nregister:{}\\r\\n");
    client->send();
    client->clear();
    this->passwd=ui->password->text();
    this->nameid=ui->nameid->text();
    ui->nameid->clear();
    ui->password->clear();
}


void login::on_login_2_clicked()        //注册
{
    MainWindow *m=new MainWindow(" "," "," ");
    m->show();
}

void login::on_pushButton_2_clicked()   //登录
{
    client->write("usrnameid:{1373474316}\\r\\npassword:{137347}\\r\\nregister:{}\\r\\n");
    this->passwd="137347";
    this->nameid="1373474316";
    client->send();
    client->clear();
}
